package com.hrt.manage.cash.dao.impl;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Cash;
import com.hrt.manage.cash.dao.CashDao;

@Repository
public class CashDaoImpl extends BaseDaoImpl<Cash> implements CashDao {

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		String hql = "select count(c) from Cash c where c.isdelete=0";
		List result = this.getHibernateTemplate().find(hql);
		if (result != null && result.size() > 0) {
			return new Long(result.get(0).toString());
		} else {
			return 0l;
		}

	}

	@Override
	public Long totalCount(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		String hql = "select count(c) from Cash c where c.isdelete=0";
		if (condition != null && condition.size() > 0) {

			for (Iterator<Entry<String, Object>> itr = condition.entrySet()
					.iterator(); itr.hasNext();) {
				Entry entry = itr.next();
				String fieldName = entry.getKey().toString();
				String value = entry.getValue().toString();
				hql = hql + " and c." + fieldName + value + " ";
			}

		}
		System.out.println("hql---------------------->" + hql);
		List result = getHibernateTemplate().find(hql);
		if (result != null && result.size() > 0) {
			return new Long(result.get(0).toString());
		} else {
			return 0l;
		}
	}

	/**
	 * 
	 */
	@Override
	public Cash getCashByid(Long cashId) {

		final String hql = "select c from Cash c where c.id=" + cashId
				+ " and c.isdelete=0";
		return getHibernateTemplate().execute(new HibernateCallback<Cash>() {

			@Override
			public Cash doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(hql);

				return (Cash) q.uniqueResult();
			}

		});

	}

	/**
	 * 
	 */
	@Override
	public List<Cash> queryByPage(int pageIndex, int pageSize) {

		List<Cash> list = new ArrayList<Cash>();

		String hql = "select c from Cash c where c.isdelete=0  order by c.createTime desc";
		
		list = this.queryByPage(hql, pageIndex, pageSize);

		return list;

	}

}
